/**
 * Created by Amaris on 2016/12/6.
 */


var util = require('../utils/util.js');
var userClass = require('../class/user.js');

module.exports = {

    create: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var user = new userClass();

        var checkResult = user.checkParams(
            [
                'weid',
                'name',
                'createUid',
                'password',
                'enable'
            ],
            params);
        if (checkResult) {
            cb(util.getErrorRes('400', checkResult), null);
            return;
        } else {
            user.init(params);
        }

        models.user.findOrCreate({
            phone: user.getParam('phone'),
            createUid: user.getParam('createUid')
        }, user.getParams(), function (err, data) {
            if (err || !data) {

                cb(null, util.getErrorRes(err.status, err.message));
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },

    getInfo: function (params, cb) {
        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var user = new userClass();

        var checkResult = user.checkParams(['rowId'], params);
        if (checkResult) {
            cb(util.getErrorRes('400', checkResult));
            return;
        } else {
            user.init(params);
        }

        models.user.findOne(user.getParams()).populate('seller').exec(function (err, data) {
            //models.user.findOne(user.getParams(), function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));

        });
    },
    getUserList: function (params, cb) {

        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = "数据库连接失败";
            cb(err, null);
            return;
        }

        models.user.find().exec(function (err, data) {
            if (err) {
                cb(util.getErrorRes(err.status, err.message));
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    /**
     * 按照seller查询users
     * @param params
     * @param cb
     */
    getUserListBySeller: function (params, cb) {

        var models = util.models;
        var query = {};
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = "数据库连接失败";
            cb(err, null);
            return;
        }
        var seller = params.seller;
        if(seller)
            query = { where: { seller: seller }, sort: 'name DESC' };

        models.user.find(query).exec(function (err, data) {
            if (err) {
                cb(util.getErrorRes(err.status, err.message));
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },

    /**
     *  根据rowIds获取用户列表
     * @param params
     * @param cb
     */
    getListByRowIds: function (params, cb) {

        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = "数据库连接失败";
            cb(err, null);
            return;
        }

        var expr = {rowId: params};

        models.user.find(expr).exec(function (err, data) {
            if (err) {
                cb(err, null);
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    //获取有效用户
    getEnableUserList: function (params, cb) {

        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = "数据库连接失败";
            cb(err, null);
            return;
        }

        models.user.find({'enable': '1'}).exec(function (err, data) {
            if (err) {
                cb(util.getErrorRes(err.status, err.message));
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    //根据卖方公司名称获取用户列表
    getUserListByCompany: function (params, cb) {

        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = "数据库连接失败";
            cb(err, null);
            return;
        }

        var user = new userClass();

        var checkResult = user.checkParams(
            ['company'],
            params);
        if (checkResult) {
            cb(util.getErrorRes('400', checkResult), null);
            return;
        } else {
            user.init(params);
        }

        models.user.find(user.getParams()).exec(function (err, data) {
            if (err) {
                cb(util.getErrorRes(err.status, err.message));
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    //删除用户
    delUer: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var rowId = params.rowId;
        models.user.destroy({'rowId': rowId}).exec(function (err, data) {
            if (err || !data) {
                cb(err, null);
                return;
            }
            if (data && data.length == 0) {
                var err = new Error();
                err.status = 500;
                err.message = '删除失败';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    //软删除用户
    softDelUer: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var rowId = params.rowId;
        models.user.update({'rowId': rowId}, {'enable': '0'}).exec(function (err, data) {
            if (err || !data) {
                cb(err, null);
                return;
            }
            if (data && data.length == 0) {
                var err = new Error();
                err.status = 500;
                err.message = '删除失败';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },

    //软删除用户
    softDelContact: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var rowId = params.rowId;
        models.user.update({'rowId': rowId}, {'enable': '-1'}).exec(function (err, data) {
            if (err || !data) {
                cb(err, null);
                return;
            }
            if (data && data.length == 0) {
                var err = new Error();
                err.status = 500;
                err.message = '删除失败';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },

    update: function (params, cb) {

        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = "数据库连接失败";
            cb(err, null);
            return;
        }

        var user = new userClass();
        var checkResult = user.checkParams(
            ['rowId', 'name'
            ], params);

        if (checkResult) {
            cb(util.getErrorRes('400', checkResult), null);
            return;
        } else {
            user.init(params);
        }


        models.user.update({
            rowId: user.getParam('rowId'),
            //createUid: user.getParam('createUid')
        }, user.getParams()).exec(function (err, data) {
            if (err || !data) {
                console.log(err);
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }
            if (data && data.length == 0) {
                cb(null, util.getErrorRes("400", "更新失败"));
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },

    getInfoByPWD: function (params, cb) {
        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var user = new userClass();

        var checkResult = user.checkParams(['weid', 'password'], params);
        if (checkResult) {
            cb(util.getErrorRes('400', checkResult), null);
            return;
        } else {
            user.init(params);
        }
        user.enable = '1';

        models.user.findOne(user.getParams()).populate('seller').exec(function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data),params);

        });
    }
};



